package com.whw.springboot.walk.service.servicelmpl;

import cn.hutool.json.JSONUtil;
import com.whw.springboot.walk.config.GlobalConfig;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.entity.User;
import com.whw.springboot.walk.entity.UserInfo;
import com.whw.springboot.walk.exception.ServerCommunicateException;
import com.whw.springboot.walk.mapper.UserInfoMapper;
import com.whw.springboot.walk.mapper.UserMapper;
import com.whw.springboot.walk.service.UserService;
import com.whw.springboot.walk.utils.JwtUtil;
import com.whw.springboot.walk.utils.WeChatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private GlobalConfig globalConfig;

    @Resource
    private  UserInfoMapper userInfoMapper;


    private Logger logger= LoggerFactory.getLogger(getClass());

/*
    @Override
    public Message<?> miniProgramLogin(String jsCode) {
        String rs = WeChatUtil.wechatLogin(jsCode, globalConfig.programAppId, globalConfig.programAppSecret);
        logger.info("微信登录：jsCode：{}，result：{}",jsCode,rs);
       if (JSONUtil.isJson(rs)) {
        String openId= JSONUtil.parseObj(rs).getStr("openid");
        logger.info("openId:{}",openId);
        if (openId==null)
            throw new ServerCommunicateException();

        User user = userMapper.selectByUserOpenid(openId);

        if (user != null) {
            // 用户已存在，则直接返回其信息
            return Message.success(null).add(JwtUtil.createJwtToken(user));
        }else {
            //用户为小程序新用户，提示注册
            return Message.fail("请先登录").add(openId);
        }

    }
*/

    @Override
    public Message<?> miniProgramLogin(String jsCode) {
        String rs = WeChatUtil.wechatLogin(jsCode, globalConfig.programAppId, globalConfig.programAppSecret);
        logger.info("微信登录：jsCode：{}，result：{}",jsCode,rs);
            String openId= JSONUtil.parseObj(rs).getStr("openid");
            logger.info("openId:{}",openId);
            if (openId==null){
                throw new ServerCommunicateException();
            }else {
                //用户为小程序新用户，提示注册
                return Message.fail("请先使用学号登录").add(openId);
            }
    }

    @Override
    public Message<?> insertUser(User user) {
        return userMapper.insertUser(user) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> insertUserAndUserInfo(User user) {
        return userMapper.insertUserAndUserInfo(user)> 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message<?> deleteByPrimaryKey(String userId){
        return userMapper.deleteByPrimaryKey(userId)>0?Message.success(null):Message.fail(null);
    }


    @Override
    public Message<List<User>> selectAllUser() {
        return Message.success(null).add(userMapper.selectAllUser());
    }


    @Override
    public Message<List<User>> selectAllUserAndInfo() {
        return Message.success(null).add(userMapper.selectAllUserAndInfo());
    }


    @Override
    public Message<User> selectByPrimaryKey(String userId) {
        return Message.success(null).add(userMapper.selectByPrimaryKey(userId));
    }

    @Override
    public Message<User> selectByPrimaryKey1(String userId) {
        return Message.success(null).add(userMapper.selectByPrimaryKey1(userId));
    }


    @Override
    public Message<User> selectByUserIdAndUserPwd(String userId, String userPwd) {
        return Message.success(null).add(userMapper.selectByUserIdAndUserPwd(userId, userPwd));
    }


    @Override
    public Message<?> updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user)>0?Message.success(null):Message.fail(null);
    }


    @Override
    public Message<?> updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user)>0?Message.success(null):Message.fail(null);
    }


    @Override
    public Message<?> changePwd(String user_id, String user_pwd) {
        return userMapper.changePwd(user_id, user_pwd)>0?Message.success(null):Message.fail(null);
    }


    @Override
    public  Message<?> Login(String user_id, String user_pwd){
        User user = userMapper.selectByUserIdAndUserPwd(user_id, user_pwd);
        if (user != null) {
            return Message.success(null).add(JwtUtil.createJwtToken(user));
        }
        return Message.fail("用户名或密码错误");
    }


    @Override
    public Message<?> addManagerUser(User user) {
        user.setUserType("0");
        UserInfo userinfo = user.getUserInfo();
        userinfo.getUserId();
        userInfoMapper.AddUserInfo(userinfo);
        return userMapper.insertUser(user)> 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message<?> banManagerUser(User user){
        return userMapper.updateByPrimaryKey(user) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message<?> changeOldStatus(User user){

        return userMapper.updateByPrimaryKey(user) >0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> selectByUserIdOldStatus(String userId) {
        String judge = userMapper.selectByPrimaryKey(userId).getUserOldStatus();
        if (judge == "1") {
            return Message.success("已加入老乡会");
        }
        return Message.fail("未加入老乡会");

    }

    @Override
    public Message<?> insertUserAndUserInfo(UserInfo userInfo) {
        return null;
    }


}
